<?php
include 'core.php';
include 'wsdl.class.php';

$user=$_SESSION['USERID'];

$res = mysql_fetch_assoc(mysql_query("SELECT crystal_users.username
									  FROM 	 crystal_users
									  WHERE  crystal_users.id=$user"));
$user_name=$res[username];


$wsdl = new wsdl('http://192.168.215.30/services/public/callcentersupportservice.asmx?wsdl', 'CallCenter', '2cc4541a13009ba5da9b90a146d10369', '192.168.200.145', $user_name, 'index.php');

class Authorization {

	private  $username;
	private  $name;
	private  $RoleId;
	private  $password;
	private  $ext;

	private  $user_id;
	private  $sess_id;

	private  $date;
	private  $ip;

	private  $time = 36000;


	/**
	 * constructor
	 */
	function Authorization() {
	}

	/**
	 * @param string $username სისტემის მომხმარებელი
	 */
	function set_username($username) {
		$this->username = $username;
	}
	function set_ext($ext) {
		$this->ext = $ext;
	}

	/**
	 * @param string $password მომხმარებლის პაროლი
	 */
	function set_password($password) {
		$this->password = $password;
	}

	/**
	 * სისტემის მომხარებლის შემოწმება
	 * @return boolean
	 */
	function checklogin() {
	    
	    
	    
	    $username = $this->username;
	    $password = $this->password;
	    
	    global $wsdl;
	    
	    $result = $wsdl->AuthenticateUser($this->username, $this->password);
	    
	    if ($result->UserId) {
	        $this->user_id     = $result->UserId;
	        $this->name        = $result->FullName;
	        $this->roleid      = $result->RoleId;
	        $this->username    = $result->UserName;

	        return true;
	    }else{
	        return false;
	    }	    

	}

	function ip()
	{
		global $REMOTE_ADDR;
		global $HTTP_X_FORWARDED_FOR, $HTTP_X_FORWARDED, $HTTP_FORWARDED_FOR, $HTTP_FORWARDED;
		global $HTTP_VIA, $HTTP_X_COMING_FROM, $HTTP_COMING_FROM;
		if (empty($REMOTE_ADDR)) {
			if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])) {
				$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
			}
			else if (!empty($_ENV) && isset($_ENV['REMOTE_ADDR'])) {
				$REMOTE_ADDR = $_ENV['REMOTE_ADDR'];
			}
			else if (@getenv('REMOTE_ADDR')) {
				$REMOTE_ADDR = getenv('REMOTE_ADDR');
			}
		}
		if (empty($HTTP_X_FORWARDED_FOR)) {
			if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
				$HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
			}
			else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
				$HTTP_X_FORWARDED_FOR = $_ENV['HTTP_X_FORWARDED_FOR'];
			}
			else if (@getenv('HTTP_X_FORWARDED_FOR')) {
				$HTTP_X_FORWARDED_FOR = getenv('HTTP_X_FORWARDED_FOR');
			}
		}
		if (empty($HTTP_X_FORWARDED)) {
			if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])) {
				$HTTP_X_FORWARDED = $_SERVER['HTTP_X_FORWARDED'];
			}
			else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED'])) {
				$HTTP_X_FORWARDED = $_ENV['HTTP_X_FORWARDED'];
			}
			else if (@getenv('HTTP_X_FORWARDED')) {
				$HTTP_X_FORWARDED = getenv('HTTP_X_FORWARDED');
			}
		}
		if (empty($HTTP_FORWARDED_FOR)) {
			if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])) {
				$HTTP_FORWARDED_FOR = $_SERVER['HTTP_FORWARDED_FOR'];
			}
			else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED_FOR'])) {
				$HTTP_FORWARDED_FOR = $_ENV['HTTP_FORWARDED_FOR'];
			}
			else if (@getenv('HTTP_FORWARDED_FOR')) {
				$HTTP_FORWARDED_FOR = getenv('HTTP_FORWARDED_FOR');
			}
		}
		if (empty($HTTP_FORWARDED)) {
			if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])) {
				$HTTP_FORWARDED = $_SERVER['HTTP_FORWARDED'];
			}
			else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED'])) {
				$HTTP_FORWARDED = $_ENV['HTTP_FORWARDED'];
			}
			else if (@getenv('HTTP_FORWARDED')) {
				$HTTP_FORWARDED = getenv('HTTP_FORWARDED');
			}
		}
		if (empty($HTTP_VIA)) {
			if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])) {
				$HTTP_VIA = $_SERVER['HTTP_VIA'];
			}
			else if (!empty($_ENV) && isset($_ENV['HTTP_VIA'])) {
				$HTTP_VIA = $_ENV['HTTP_VIA'];
			}
			else if (@getenv('HTTP_VIA')) {
				$HTTP_VIA = getenv('HTTP_VIA');
			}
		}
		if (empty($HTTP_X_COMING_FROM)) {
			if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])) {
				$HTTP_X_COMING_FROM = $_SERVER['HTTP_X_COMING_FROM'];
			}
			else if (!empty($_ENV) && isset($_ENV['HTTP_X_COMING_FROM'])) {
				$HTTP_X_COMING_FROM = $_ENV['HTTP_X_COMING_FROM'];
			}
			else if (@getenv('HTTP_X_COMING_FROM')) {
				$HTTP_X_COMING_FROM = getenv('HTTP_X_COMING_FROM');
			}
		}
		if (empty($HTTP_COMING_FROM)) {
			if (!empty($_SERVER) && isset($_SERVER['HTTP_COMING_FROM'])) {
				$HTTP_COMING_FROM = $_SERVER['HTTP_COMING_FROM'];
			}
			else if (!empty($_ENV) && isset($_ENV['HTTP_COMING_FROM'])) {
				$HTTP_COMING_FROM = $_ENV['HTTP_COMING_FROM'];
			}
			else if (@getenv('HTTP_COMING_FROM')) {
				$HTTP_COMING_FROM = getenv('HTTP_COMING_FROM');
			}
		}

		if (!empty($REMOTE_ADDR)) {
			$direct_ip = $REMOTE_ADDR;
		}

		$proxy_ip	 = '';
		if (!empty($HTTP_X_FORWARDED_FOR)) {
			$proxy_ip = $HTTP_X_FORWARDED_FOR;
		} else if (!empty($HTTP_X_FORWARDED)) {
			$proxy_ip = $HTTP_X_FORWARDED;
		} else if (!empty($HTTP_FORWARDED_FOR)) {
			$proxy_ip = $HTTP_FORWARDED_FOR;
		} else if (!empty($HTTP_FORWARDED)) {
			$proxy_ip = $HTTP_FORWARDED;
		} else if (!empty($HTTP_VIA)) {
			$proxy_ip = $HTTP_VIA;
		} else if (!empty($HTTP_X_COMING_FROM)) {
			$proxy_ip = $HTTP_X_COMING_FROM;
		} else if (!empty($HTTP_COMING_FROM)) {
			$proxy_ip = $HTTP_COMING_FROM;
		}

		if (empty($proxy_ip)) {
			$this->ip =  $direct_ip;
		} else {
			$is_ip = preg_match('|^([0-9]{1,3}\.){3,3}[0-9]{1,3}|', $proxy_ip, $regs);
			if ($is_ip && (count($regs) > 0)) {
				$this->ip =  $regs[0];
			} else {
				$this->ip =  'unknow';
			}
		}
	}

	/**
	 * სესიის მონაცემების შენახვა
	 */
	function savelogin() {
		$_SESSION['USERID']       = $this->user_id;
		$_SESSION['EXTENSION']    = $this->ext;
		session_regenerate_id();
		$this->sess_id 	          = session_id();
		$this->date		          = date("Y-m-d H:i:s");
		
		$username = $this->name;
		
		mysql_query("INSERT INTO `test`
		(`id`)
		VALUES ('$username')");
		
		mysql_query("INSERT INTO `crystal_users` 
		                    (`id`, `username`,`group_id`, `name`, `last_extension`, `logged`)
            		 VALUES ('$this->user_id', '$this->username', '$this->roleid' , '$username', '$this->ext', 1)
            		 ON DUPLICATE KEY
            		 UPDATE `username`='$this->username', `group_id`='$this->roleid', `name`='$username', `last_extension`='$this->ext', logged=1");
 		
 		$work_id = mysql_fetch_row(mysql_query("
             		                           SELECT actived FROM `worker_action` WHERE person_id = $this->user_id AND DATE(start_date) = DATE(NOW())
             		                           "));
 		
 		if($work_id[0] == ''){
 		mysql_query("INSERT INTO `worker_action` 
                    (`person_id`, `start_date`, `actived`)
                    VALUES
                    ('$this->user_id', NOW(), '1')
 		            ");
 		}elseif ($work_id[0] == 2){
 		    mysql_query("
             		    UPDATE  `worker_action` SET
                     		    `timeout_end_date`=NOW(),
                     		    `actived`='3'
             		    WHERE   `actived`= 2 AND `person_id`='$this->user_id'
             		    ");
 		}
		mysql_query("INSERT INTO `users_log` (`user_id`, `extension`, `action`, `datatime`, `ip`)
                          VALUES ('$this->user_id', $_SESSION[EXTENSION], 'login', '$this->date', '$this->ip')
		           ");
	}


	function logout(){

	    $datetime = date("Y-m-d H:i:s");
	    mysql_query("INSERT INTO `users_log` (`user_id`, `action`, `datatime`)
	                       VALUES ($_SESSION[USERID], 'logout', '$datetime')
	               ");
	    
 		mysql_query("UPDATE `crystal_users`
 						SET	`logged` 	= 0
 					  WHERE	`id` = $_SESSION[USERID]
 					");

		session_start();
		session_destroy();
		unset($_SESSION['USERID']);
		return true;
	}
}

?>